package hero.mps.web.system_user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hero.mps.web.sys_user_role.entity.SysUserRole;
import hero.mps.web.sys_user_role.service.SysUserRoleService;
import hero.mps.web.system_user.entity.PageParam;
import hero.mps.web.system_user.entity.SysUser;
import hero.mps.web.system_user.mapper.SysUserMapper;
import hero.mps.web.system_user.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;
@RequiredArgsConstructor
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    private final SysUserRoleService sysUserRoleService;
    @Override
    public IPage<SysUser> list(PageParam pageParam) {
        // 构建分页对象
        IPage<SysUser> sysUserPage = new Page<>();
        sysUserPage.setCurrent(pageParam.getCurrentPage());
        sysUserPage.setPages(pageParam.getPageSize());
        QueryWrapper<SysUser> query = new QueryWrapper<>();
        // 构建查询条件
        if (StringUtils.isNotEmpty(pageParam.getNickName())){
            query.lambda()
                    .like(SysUser::getNickName, pageParam.getNickName());
        }
        if (StringUtils.isNotEmpty(pageParam.getPhone())){
            query.lambda()
                    .like(SysUser::getPhone, pageParam.getPhone());
        }
        return baseMapper.selectPage(sysUserPage, query);
    }

    @Override
    @Transactional
    public void add(SysUser sysUser) {
        // 保存用户
        int insert = this.baseMapper.insert(sysUser);
        // 保存角色
        if (insert > 0) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(sysUser.getUserId());
            sysUserRole.setRoleId(sysUser.getRoleId());
            sysUserRoleService.save(sysUserRole);
        }
    }

    @Override
    @Transactional
    public void edit(SysUser sysUser) {
        // 编辑用户
        int update = this.baseMapper.updateById(sysUser);
        // 编辑角色 先删除再插入
        if (update > 0) {
//            先删除
            QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SysUserRole::getUserId, sysUser.getUserId());
            sysUserRoleService.remove(queryWrapper);
//            再插入
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(sysUser.getUserId());
            sysUserRole.setRoleId(sysUser.getRoleId());
            sysUserRoleService.save(sysUserRole);
        }
    }

}
